using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.PictureAcquisition;

namespace PictureAcquisitionApplication
{
    public partial class DeviceItemsDetails : Form
    {
        #region Globals
        PictureAcquisitionManager pictureAquisitionManager = null;
        string deviceIdNew = string.Empty;
#endregion
        #region Constructor
        public DeviceItemsDetails()
        {
            InitializeComponent();
        }
        public DeviceItemsDetails(PictureAcquisitionManager manager, string deviceId)
        {
            deviceIdNew = deviceId;
            pictureAquisitionManager = manager;
            InitializeComponent();
            GetDeviceItemsDetails(deviceIdNew);

        }
        #endregion
        #region Helper Methods
        private void GetDeviceItemsDetails(string deviceIdNew)
        {
            try
            {
                List<string> deviceItems = new List<string>();
                deviceItems = pictureAquisitionManager.GetDeviceItems(deviceIdNew);
                foreach(string dev in deviceItems)
                {
                    dgViewItemName.Rows.Add(dev);
                }

            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }

        }
        private void DeleteDeviceItem(string deviceIdNew)
        {
            try
            {
                DataGridViewSelectedRowCollection rows = dgViewItemName.SelectedRows;

                foreach (DataGridViewRow r in rows)
                {
                    string itemName = r.Cells[0].Value.ToString();
                    pictureAquisitionManager.DeleteItem(itemName, deviceIdNew);
                    dgViewItemName.Rows.Remove(r);
                }
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }
        private void AcquireSelectedDeviceItems(string deviceIdNew)
        {
            try
            {
                DataGridViewSelectedRowCollection rows = dgViewItemName.SelectedRows;
                List<string> selectedItems = new List<string>();

                foreach (DataGridViewRow r in rows)
                {
                    string itemName = r.Cells[0].Value.ToString();
                    selectedItems.Add(itemName);
                }

                FolderBrowserDialog fileBrowserDialog = new FolderBrowserDialog();
                fileBrowserDialog.Description = "Choose folder to import selected files";
                fileBrowserDialog.ShowNewFolderButton = true;

                if (DialogResult.OK == fileBrowserDialog.ShowDialog())
                {
                    pictureAquisitionManager.AcquireItems(deviceIdNew, selectedItems, fileBrowserDialog.SelectedPath + "\\");
                }
  
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }
        #endregion
        #region Event Handler
        private void btnRefreshDeviceItems_Click(object sender, EventArgs e)
        {
            try
            {
                dgViewItemName.Rows.Clear();
                GetDeviceItemsDetails(deviceIdNew);
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        private void btnDeleteDeviceItems_Click(object sender, EventArgs e)
        {
            try
            {
                DeleteDeviceItem(deviceIdNew);
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }
        #endregion

        private void btnCloseDeviceItems_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DeviceItemsDetails_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

        private void btnDeviceItemDetailsImport_Click(object sender, EventArgs e)
        {
            AcquireSelectedDeviceItems(deviceIdNew);
        }
     
    }

}